<template>
  <div v-if="!isshowwz">
    <p>账号<input type="text" name="username" id="username" v-model="username"></p>
  <p>密码<input type="password" name="password" id="password" v-model="password"></p>
  <p>服务器id<input type="text" name="serveID" id="serveID" v-model="serveID"></p>
  <button type="button" @click="Submitlogin">登录</button>
  </div>
<div v-if="isshowwz">{{ hello }}</div>
</template>
<script>
import TencentCloudChat  from '@tencentcloud/chat';
import Crypto from 'crypto-js';
import Axios from 'axios';
import  myWorker  from "worker-loader!./worker.js";
import moment from 'moment';
export default {
  name: 'App',
  data(){
    return {
      hello:'',
      appid:0,
      username:'',
      password:'',
      serveID:'',
      userSig:"",
      userid:"",
      key:Crypto.enc.Utf8.parse("666888\0\0\0\0\0\0\0\0\0\0"),
      config:{},
      superAdmin:"",
      goup:[],
      Worker:null,
      Worker2:null,
      adv1:"",
      adv2:"",
      adv3:"",
      iskj:true,
      isyc:true,
      isszyc:true,
      iszlyc:true,
      iszlsz:true,
      iszlzs:true,
      iszlsj:true,
      kjurl:"https://api.ts28.com.cn/getjndkj",
      kjdata:{},// 开奖数据
      dtime:0,// 开奖倒计时
      newissue:0,// 新开奖期号
      oldissue:0, // 上一期开奖期号
      alg : '2+3+5', // 算法 1
      alg2 : '7+3+5', // 算法 2
      alg3  : '6+1+3', // 算法3
      algdata:[], // 算法数据
      setdtime:null,
      algindex:1,// 组合算法
      algindex2:1, // 杀组算法
      chat: {},// 机器人标识
      qingchu:[],
      daycount:0,
      isshowwz:false
    }
  },
  components: {
    
  },
  created(){
    let grondata =  localStorage.getItem('userdata')
    if(grondata){
      this.goup = JSON.parse(grondata)
    }else{
      this.goup = []
    }
    let admindata = localStorage.getItem('useradmin')
    if(admindata){
      this.superAdmin = admindata
    }else{
      this.superAdmin =''
    }
  },
  // 函数
  methods: {
    Submitlogin(){
      let data = {
        password: this.password,
        serverId: this.serveID,
        userName: this.username,
      }
      let login = Axios.create({
        baseURL: 'https://api.ts28.com.cn',
      })
      login.post('/wqlogin', data).then(res => {
              if (res.data.code == 200) {
                this.accid = res.data.data.accid
                this.toekn = res.data.data.token
                this.imappid = parseInt(res.data.data.imAppid)
                // 登录腾讯im
                let options = {
                  SDKAppID: parseInt(res.data.data.imAppid)
                }
                this.chat = TencentCloudChat.create(options);
                let promise = this.chat.login({ userID: res.data.data.accid, userSig: res.data.data.token });
                promise.then((imResponse) => {
                  this.hello = res.data.data.accid
                  setTimeout(() => {
                    this.chat.on(TencentCloudChat.EVENT.MESSAGE_RECEIVED, this.getMsg)
                    this.chat.on(TencentCloudChat.EVENT.KICKED_OUT, this.onKickedOut);
                    this.chat.on(TencentCloudChat.EVENT.MESSAGE_RECEIVED, this.onMessageReceived);
                    this.isshowwz = true
                  }, 1000)
                  if (imResponse.data.repeatLogin === true) {
                    // 标识账号已登录，本次登录操作为重复登录。
                    this.chat.destroy()
                    this.Submitlogin()
                  }
                }).catch((imError) => {
                  console.log(imError)
                  this.isshowwz = false
                });
              } else {
                this.isshowwz = false
              }
            }).catch(() => {
              this.isshowwz = false
            })
    },
    getMsg(event){
      const messageList = event.data;
      messageList.forEach((message) => {
      let msg = this.textPa(message.payload.text)
      if (message.type === TencentCloudChat.TYPES.MSG_TEXT) {
        // 文本消息 
        if(message.conversationType == "GROUP"){
          // 群组信息
          if(msg == "开始" && message.from == this.superAdmin){
            // 超级管理员添加群组信息
            // 判断群组是否已经添加
            if(!this.goup.some(item => item.id === message.to)){
              // 不存在群组，添加
              this.goup.push({
                id:message.to,
                isvip:false,
                vipdate:"",
                vipadv1:"",
                vipadv2:"",
                vipadv3:"",
                qiandao:[],
                shua:[],
                algindex:1,
                isszyc:true,
                algindex2:1,
                admin:[],
                iskj:true,
                isyc:true,
                iszlyc:true,
                iszlsz:true,
                iszlzs:true,
                iszlsj:true,
                hhy:'',
                dingshi:[]
              })
              this.sendMsgG("开启成功！",message.to)
            }else{
              this.sendMsgG("机器人开启中，请勿重复发送",message.to)
            }
          }
          if(this.goup.some(item => item.id === message.to)){
            // 群预测功能的消息处理
            let toArray = this.goup.filter(obj => obj.id === message.to)
            let goupIndex = this.goup.findIndex(item=>item.id === message.to)
            if(message.from == this.superAdmin){
              // 升级vip群
              if(this.startsWithChar(msg,"升级vip")){
                let day = parseInt(msg.split("-")[1])
                let thirtyDaysLater = new Date();
                thirtyDaysLater.setDate(thirtyDaysLater.getDate() + day);
                let dateThirtyDaysLater = thirtyDaysLater.toISOString().split('T')[0];
                this.goup[goupIndex].vipdate = dateThirtyDaysLater
                this.goup[goupIndex].isvip = true
                if(this.goup[goupIndex].isvip){
                  this.sendMsgG("升级成功，到期时间为："+this.goup[goupIndex].vipdate,message.to)
                }else{
                  this.sendMsgG("升级失败，请重新发送",message.to)
                }
              }
            }


            if(msg === "帮助"){
              let str = this.helpMsg(this.goup[goupIndex].isvip)
              this.sendMsgG(str,message.to)
            }
            // 刷屏功能结束
            if(toArray[0].isvip){
              // 是vip群，可以查看切换算法
             // 防止刷屏功能
              if(msg == "预测1"){
                if(this.goup[goupIndex].iszlyc){
                  this.ycdata(1)
                  let msg = this.ycmsg(1)+'\n'+'===================='
                  this.sendMsgG(msg,message.to)
                  this.sendMsgG(this.goup[goupIndex].vipadv2,message.to)
                }
              }
              if(msg == "预测2"){
                if(this.goup[goupIndex].iszlyc){
                  this.ycdata(2)
                  let msg = this.ycmsg(2)+'\n'+'===================='
                  this.sendMsgG(msg,message.to)
                  this.sendMsgG(this.goup[goupIndex].vipadv2,message.to)
                }
              }
              if(msg == "预测3"){
                if(this.goup[goupIndex].iszlyc){
                  this.ycdata(3)
                  let msg = this.ycmsg(3)+'\n'+'===================='
                  this.sendMsgG(msg,message.to)
                  this.sendMsgG(this.goup[goupIndex].vipadv2,message.to)
                }
              }
              if(msg == "杀组1"){
                if(this.goup[goupIndex].iszlsz){
                  this.ycdata(1)
                  let msg = this.szycmsg(1)+'\n'+'===================='
                  this.sendMsgG(msg,message.to)
                  this.sendMsgG(this.goup[goupIndex].vipadv3,message.to)
                }
              }
              if(msg == "杀组2"){
                if(this.goup[goupIndex].iszlsz){
                  this.ycdata(2)
                  let msg = this.szycmsg(2)+'\n'+'===================='
                  this.sendMsgG(msg,message.to)
                  this.sendMsgG(this.goup[goupIndex].vipadv3,message.to)
                }
              }
              if(msg == "杀组3"){
                if(this.goup[goupIndex].iszlsz){
                  this.ycdata(3)
                  let msg = this.szycmsg(3)+'\n'+'===================='
                  this.sendMsgG(msg,message.to)
                  this.sendMsgG(this.goup[goupIndex].vipadv3,message.to)
                }
              }
              if(msg == "2" || msg == "预测"){
                if(this.goup[goupIndex].iszlyc){
                  this.ycdata(this.goup[goupIndex].algindex)
                  let msg = this.ycmsg(this.goup[goupIndex].algindex)
                  this.sendMsgG(msg,message.to)
                  this.sendMsgG(this.goup[goupIndex].vipadv2,message.to)
                }
              }
              if(msg == "1" || msg == "杀组"){
                if(this.goup[goupIndex].iszlsz){
                  this.ycdata(this.goup[goupIndex].algindex2)
                  let msg = this.szycmsg(this.goup[goupIndex].algindex2)
                  this.sendMsgG(msg,message.to)
                  this.sendMsgG(this.goup[goupIndex].vipadv3,message.to)
                }
              }
              if(msg == "3" || msg == "走势" || msg == "历史"){
                if(this.goup[goupIndex].iszlzs){
                  let msg = this.lsmsg()+'\n'+'===================='
                  this.sendMsgG(msg,message.to)
                  this.sendMsgG(this.goup[goupIndex].vipadv1,message.to)
                }
              }
              if(msg == "4" || msg == "数据" || msg == "统计"){
                if(this.goup[goupIndex].iszlsj){
                  let msg = this.tongji()+'\n'+'===================='
                  this.sendMsgG(msg,message.to)
                  this.sendMsgG(this.goup[goupIndex].vipadv1,message.to)
                }
              }
            }else{
              if(msg == "2" || msg == "预测"){
                if(this.iszlyc){
                  this.ycdata(this.goup[goupIndex].algindex)
                  let msg = this.ycmsg(this.goup[goupIndex].algindex)+'\n'+'===================='
                  this.sendMsgG(msg,message.to)
                  this.sendMsgG(this.adv2,message.to)
                }
              }
              if(msg == "1" || msg == "杀组"){
                if(this.iszlsz){
                  this.ycdata(this.goup[goupIndex].algindex2)
                  let msg = this.szycmsg(this.goup[goupIndex].algindex2)+'===================='
                  this.sendMsgG(msg,message.to)
                  this.sendMsgG(this.adv3,message.to)
                }
              }
              if(msg == "3" || msg == "走势" || msg == "历史"){
                if(this.iszlzs){
                  let msg = this.lsmsg()+'\n'+'===================='
                  this.sendMsgG(msg,message.to)
                  this.sendMsgG(this.adv1,message.to)
                }
              }
              if(msg == "4" || msg == "数据" || msg == "统计"){
                if(this.iszlsj){
                  let msg = this.tongji()+'\n'+'===================='
                  this.sendMsgG(msg,message.to)
                  this.sendMsgG(this.adv1,message.to)
                }
              }
            }
            // 处理超级管理员群操作
            if(toArray[0].isvip && message.from==this.superAdmin){
              // 添加操作员
              if(this.startsWithChar(msg,"添加操作人")){
                if(message.atUserList.length !=0){
                  // 艾特有操作人
                  let oldadmin = this.goup[goupIndex].admin
                  let addadmin = message.atUserList
                  let newadmin = this.mergeAndDedupeArrays(oldadmin,addadmin)
                  this.goup[goupIndex].admin = newadmin
                  this.sendMsgG("添加成功，添加后的操作人数为："+this.goup[goupIndex].admin.length+"人",message.to)
                }
              }
              // 删除操作人
              if(this.startsWithChar(msg,"删除操作人")){
                if(message.atUserList.length !=0){
                  // 艾特有操作人
                  let readmin = message.atUserList
                  for(let i=0;i<readmin.length;i++){
                    this.goup[goupIndex].admin=this.goup[goupIndex].admin.filter(item=>item !== readmin[i])
                  }
                  this.sendMsgG("删除成功，删除后的操作人数为："+this.goup[goupIndex].admin.length+"人",message.to)
                }
              }
              // 查看vip天数
              if(msg == "查看vip天数"){
                let vipdq = new Date(this.goup[goupIndex].vipdate)
                let thirtyDaysLater = new Date();
                thirtyDaysLater.setDate(thirtyDaysLater.getDate())
                let dq = new Date(thirtyDaysLater.toISOString().split('T')[0])
                let day = this.getDaysBetweenDates(dq,vipdq)
                this.sendMsgG("本群vip剩余天数："+day+"天",message.to)
              }
              if(this.startsWithChar(msg,"续费vip")){
                let day = parseInt(msg.split("-")[1])
                let thirtyDaysLater = new Date(this.goup[goupIndex].vipdate);
                thirtyDaysLater.setDate(thirtyDaysLater.getDate() + day);
                let dateThirtyDaysLater = thirtyDaysLater.toISOString().split('T')[0];
                this.goup[goupIndex].vipdate = dateThirtyDaysLater
                this.goup[goupIndex].isvip = true
                if(this.goup[goupIndex].isvip){
                  this.sendMsgG("续费成功，到期时间为："+this.goup[goupIndex].vipdate,message.to)
                }else{
                  this.sendMsgG("续费失败，请重新发送",message.to)
                }
              }
            }
            if(msg == "签到"){
              if(toArray[0].isvip){
              // 是vip
              // 签到功能开启
              // 判断是否重复签到
              let dayDate = this.getYYYYMMDD(new Date())
              let qiandaoIndex = this.goup[goupIndex].qiandao.findIndex(item => item.date === dayDate && item.userId === message.from)
              if(qiandaoIndex == -1){
                // 不存在，签到
                // 处理连续签到天数
                let currentDate = new Date();  // 获取当前日期和时间
                currentDate.setDate(currentDate.getDate() - 1);  // 获取前一天的日期
                let curr  = this.getYYYYMMDD(currentDate)
                let qiandaoU = this.goup[goupIndex].qiandao.filter(item=>item.userId === message.from && item.date === curr)
                console.log(qiandaoU)
                if(qiandaoU.length == 0){
                  // 不存在连续签到 == 1
                  this.goup[goupIndex].qiandao.push({
                    date:dayDate,
                    userId:message.from,
                    nick:message.nick,
                    lianxu:1
                  })
                }else{
                  // 是连续签到，要加1
                  this.goup[goupIndex].qiandao.push({
                    date:dayDate,
                    userId:message.from,
                    nick:message.nick,
                    lianxu:parseInt(qiandaoU[qiandaoU.length-1].lianxu)+1
                  })
                }
                let qiandaoyue = this.goup[goupIndex].qiandao.filter(item=>item.userId === message.from && item.date.slice(0, 6) === dayDate.slice(0,6))
                let qiandaolianxu = this.goup[goupIndex].qiandao.filter(item=>item.userId === message.from)
                this.sendMsgAt("@"+message.nick+" 签到成功，本月累计签到"+qiandaoyue.length+"天，连续签到"+qiandaolianxu[qiandaolianxu.length-1].lianxu+"天",message.to,[message.from])
              }else{
                // 存在，发送重复签到提醒
                this.sendMsgAt("@"+message.nick+" 今日已签到，请勿重复签到",message.to,[message.from])
              }          

              }
            }
            // vip群功能命令处理
            if(toArray[0].isvip &&  toArray[0].admin.indexOf(message.from) != -1 || message.from == this.superAdmin){
              // vip
              let fg = this.splitByNumbers(msg)
              if(fg[0]==="开始清理" && fg[2]==="天未签到用户" ){
                this.qingchu = []
                let day = parseInt(fg[1])
                this.getnum(message.to,goupIndex,day).then(result=>{
                  this.qingchu=result
                }).catch(()=>{
                  this.sendMsgG("查找未签到用户失败",message.to)
                })
                this.sendMsgG("正在查找，请稍等",message.to)
                setTimeout(()=>{
                  console.log(this.qingchu)
                  if(this.qingchu.length != 0){
                    this.sendMsgG("找到"+this.qingchu.length+"位群员，确认踢出群聊请回复[确认清除]",message.to)
                  }else{
                      this.sendMsgG("未找到"+day+"天未签到群员",message.to)
                  }
                },5000)
              }
              if(msg == '确认清除'){
                if(this.qingchu.length!=0){
                  let chenggong = 0
                  let shibai = 0
                  this.qingchu.forEach(list=>{
                    this.chat.deleteGroupMember({
                      groupID: message.to,
                      userIDList:list,
                      reason: '你不活跃，我要踢你！',
                    }).then(()=>{
                     // this.sendMsgG("清除成功",message.to)
                     chenggong++
                    }).catch(()=>{
                    //  this.sendMsgG("清除失败，请重试",message.to)
                      shibai++
                    })
                  })
                  this.sendMsgG("清除成功："+chenggong+'人'+'\n'+"清除失败:"+shibai+'人',message.to)
                  this.qingchu = []
                }
              }
              let fgmsg = msg.split("-")
              if(fgmsg[0] == "设置广告1"){
                this.goup[goupIndex].vipadv1 = fgmsg[1]
                this.sendMsgG("设置成功，设置后的广告1为："+this.goup[goupIndex].vipadv1,message.to)
              }
              if(fgmsg[0] == "设置广告2"){
                this.goup[goupIndex].vipadv2 = fgmsg[1]
                this.sendMsgG("设置成功，设置后的广告2为："+this.goup[goupIndex].vipadv2,message.to)
              }
              if(fgmsg[0] == "设置广告3"){
                this.goup[goupIndex].vipadv3 = fgmsg[1]
                this.sendMsgG("设置成功，设置后的广告3为："+this.goup[goupIndex].vipadv3,message.to)
              }
              if(fgmsg[0] == "设置欢迎语"){
                this.goup[goupIndex].hhy = fgmsg[1]
                this.sendMsgG("设置成功，设置后的欢迎语为："+this.goup[goupIndex].hhy,message.to)
              }
              if(fgmsg[0] == "删除定时任务"){
                let indexx = fgmsg[1].split(",")
                indexx = indexx.map(str=>parseInt(str))
                console.log(indexx)
                this.goup[goupIndex].dingshi = this.goup[goupIndex].dingshi.filter((item,index)=>indexx.indexOf(index) === -1)
                let resmg = '删除成功，删除后的定时任务为：'
                this.goup[goupIndex].dingshi.forEach((item,index)=>{
                  resmg = resmg+'\n'+'id:'+index+'---'+item.text+'---发送间隔:'+item.ss+'秒'
                })
                this.sendMsgG(resmg,message.to)
              }
              if(fgmsg[0] == "添加定时任务"){
                if(this.goup[goupIndex].dingshi){
                  this.goup[goupIndex].dingshi.push({
                    date:moment().format('YYYY-MM-DD HH:mm:ss'),
                    ss:fgmsg[2],
                    text:fgmsg[1]
                  })
                }else{
                  this.goup[goupIndex].dingshi=[]
                  this.goup[goupIndex].dingshi.push({
                    date:moment().format('HH:mm:ss'),
                    ss:fgmsg[2],
                    text:fgmsg[1]
                  })
                }
                let resmg = '添加成功，添加后的定时任务为：'
                this.goup[goupIndex].dingshi.forEach((item,index)=>{
                  resmg = resmg+'\n'+'id:'+index+'---'+item.text+'---发送间隔:'+item.ss+'秒'
                })
                this.sendMsgG(resmg,message.to)
              }
              if(msg == "查看定时任务"){
                let resmg = '定时任务列表：'
                this.goup[goupIndex].dingshi.forEach((item,index)=>{
                  resmg = resmg+'\n'+'id:'+index+'---'+item.text+'---发送间隔:'+item.ss+'秒'
                })
                this.sendMsgG(resmg,message.to)
              }
              if(msg == "开奖开关"){
                this.goup[goupIndex].iskj = !this.goup[goupIndex].iskj
                if(this.goup[goupIndex].iskj){
                  this.sendMsgG("自动开奖开关--开启中",message.to)
                }else{
                  this.sendMsgG("自动开奖开关--关闭中",message.to)
                }
              }
              if(msg == "杀组开关"){
                this.goup[goupIndex].isszyc = !this.goup[goupIndex].isszyc
                if(this.goup[goupIndex].isszyc){
                  this.sendMsgG("自动发送杀组预测开关--开启中",message.to)
                }else{
                  this.sendMsgG("自动发送杀组预测开关--关闭中",message.to)
                }
              }
              if(msg == "预测开关"){
                this.goup[goupIndex].isyc = !this.goup[goupIndex].isyc
                if(this.goup[goupIndex].isyc){
                  this.sendMsgG("自动发送预测开关--开启中",message.to)
    
                }else{
                  this.sendMsgG("自动发送预测开关--关闭中",message.to)
                }
              }
              if(msg == "预测指令开关"){
                this.goup[goupIndex].iszlyc = !this.goup[goupIndex].iszlyc
                if(this.goup[goupIndex].iszlyc){
                  this.sendMsgG("预测指令开关--开启中",message.to)
                }else{
                  this.sendMsgG("预测指令开关--关闭中",message.to)
                }
              }
              if(msg == "杀组指令开关"){
                this.goup[goupIndex].iszlsz = !this.goup[goupIndex].iszlsz
                if(this.goup[goupIndex].iszlsz){
                  this.sendMsgG("杀组指令开关--开启中",message.to)
                }else{
                  this.sendMsgG("杀组指令开关--关闭中",message.to)
                }
              }

              if(msg == "走势指令开关"){
                this.goup[goupIndex].iszlzs = !this.goup[goupIndex].iszlzs
                if(this.goup[goupIndex].iszlzs){
                  this.sendMsgG("走势指令开关--开启中",message.to)
                  
                }else{
                  this.sendMsgG("走势指令开关--关闭中",message.to)
                
                }
              }
              if(msg == "数据指令开关"){
                this.goup[goupIndex].iszlsj = !this.goup[goupIndex].iszlsj
                if(this.goup[goupIndex].iszlsj){
                  this.sendMsgG("数据指令开关--开启中",message.to)
                
                }else{
                  this.sendMsgG("数据指令开关--关闭中",message.to)
          
                }
              }
              if(msg == "切换算法1"){
                this.goup[goupIndex].algindex = 1
                this.sendMsgG("已切换组合预测至算法1",message.to)
              }
              if(msg == "切换算法2"){
                this.goup[goupIndex].algindex = 2
                this.sendMsgG("已切换组合预测至算法2",message.to)
              }
              if(msg == "切换算法3"){
                this.goup[goupIndex].algindex = 3
                this.sendMsgG("已切换组合预测至算法3",message.to)
              }
              if(msg == "切换杀组算法1"){
                this.goup[goupIndex].algindex2 = 1
                this.sendMsgG("已切换杀组预测至算法1",message.to)
              }
              if(msg == "切换杀组算法2"){
                this.goup[goupIndex].algindex2 = 2
                this.sendMsgG("已切换杀组预测至算法2",message.to)
              }
              if(msg == "切换杀组算法3"){
                this.goup[goupIndex].algindex2 = 3
                this.sendMsgG("已切换杀组预测至算法3",message.to)
              }
            }
          }
        }else{
          // 私聊信息
          if(msg == "创建"){
            if(this.superAdmin ==""){
              // 没有超级管理员，添加为超级管理员
              this.superAdmin = message.from
              this.sendMsgC("添加超级管理员成功："+message.nick,message.from)
            }
          }
          if(message.from == this.superAdmin){
            // 超级管理员命令
            if(msg == "开奖开关"){
              this.iskj = !this.iskj
              if(this.iskj){
                this.sendMsgC("自动开奖开关--开启中",message.from)
                
              }else{
                this.sendMsgC("自动开奖开关--关闭中",message.from)
              }
            }
            if(msg == "预测开关"){
              this.isyc = !this.isyc
              if(this.isyc){
                this.sendMsgC("自动发送预测开关--开启中",message.from)
  
              }else{
                this.sendMsgC("自动发送预测开关--关闭中",message.from)
              }
            }
            if(msg == "杀组开关"){
              this.isszyc = !this.isszyc
              if(this.isszyc){
                this.sendMsgC("自动发送杀组预测开关--开启中",message.from)
  
              }else{
                this.sendMsgC("自动发送杀组预测开关--关闭中",message.from)
              }
            }
            if(msg == "预测指令开关"){
              this.iszlyc = !this.iszlyc
              if(this.iszlyc){
                this.sendMsgC("预测指令开关--开启中",message.from)
              }else{
                this.sendMsgC("预测指令开关--关闭中",message.from)
              }
            }
            if(msg == "杀组指令开关"){
              this.iszlsz = !this.iszlsz
              if(this.iszlsz){
                this.sendMsgC("杀组指令开关--开启中",message.from)
           
              }else{
                this.sendMsgC("杀组指令开关--关闭中",message.from)
              }
            }

            if(msg == "走势指令开关"){
              this.iszlzs = !this.iszlzs
              if(this.iszlzs){
                this.sendMsgC("走势指令开关--开启中",message.from)
                
              }else{
                this.sendMsgC("走势指令开关--关闭中",message.from)
               
              }
            }
            if(msg == "数据指令开关"){
              this.iszlsj = !this.iszlsj
              if(this.iszlsj){
                this.sendMsgC("数据指令开关--开启中",message.from)
               
              }else{
                this.sendMsgC("数据指令开关--关闭中",message.from)
         
              }
            }
            if(msg == "切换算法1"){
              this.algindex = 1
              this.sendMsgC("已切换组合预测至算法1",message.from)
            }
            if(msg == "切换算法2"){
              this.algindex = 2
              this.sendMsgC("已切换组合预测至算法2",message.from)
            }
            if(msg == "切换算法3"){
              this.algindex = 3
              this.sendMsgC("已切换组合预测至算法3",message.from)
            }

            if(msg == "切换杀组算法1"){
              this.algindex2 = 1
              this.sendMsgC("已切换杀组预测至算法1",message.from)
            }
            if(msg == "切换杀组算法2"){
              this.algindex2 = 2
              this.sendMsgC("已切换杀组预测至算法2",message.from)
            }
            if(msg == "切换杀组算法3"){
              this.algindex2 = 3
              this.sendMsgC("已切换杀组预测至算法3",message.from)
            }
            let fgmsg = msg.split("-")
            if(fgmsg[0] == "设置广告1"){
                this.adv1 = fgmsg[1]
                this.sendMsgC("设置成功，设置后的广告1为："+this.adv1,message.from)
              }
              if(fgmsg[0] == "设置广告2"){
                this.adv2 = fgmsg[1]
                this.sendMsgC("设置成功，设置后的广告2为："+this.adv2,message.from)
              }
              if(fgmsg[0] == "设置广告3"){
                this.adv3 = fgmsg[1]
                this.sendMsgC("设置成功，设置后的广告3为："+this.adv3,message.from)
              }
          }
        }
      } 
      })
    },
    onKickedOut(){
      this.chat.destroy()
      this.Submitlogin()
    },
    onMessageReceived (event){
  // event.data - 存储 Message 对象的数组 - [Message]
      const messageList = event.data;
      // 这里是群加入成员消息
      messageList.forEach((message) => {
        if(message.type === TencentCloudChat.TYPES.MSG_GRP_TIP){
          if(message.payload.operationType == 1){
          // 有人进群
            let group = this.goup.filter(item=>item.id == message.to)
            if(group.length!=0){
              if(group[0].isvip && group[0].hhy!=''){
                // 判断是vip群，有设置欢迎语
                  message.payload.memberList.forEach(list=>{
                    this.sendMsgAt("@"+list.nick+group[0].hhy,message.to,[list.userID])
                  })
              }
            }
          }
        }
      })
    },
    textPa(bastr){
      let srcs = bastr;
        let decrypt = Crypto.AES.decrypt(srcs, this.key, {
            mode: Crypto.mode.ECB,
            padding: Crypto.pad.Pkcs7
        });
        return decrypt.toString(Crypto.enc.Utf8)
    },
    getWs(num){
      return parseInt(parseInt(num)%10);
    },
    getDx(num){
      if(parseInt(num)>13){
          return '大'
      }else{
          return '小'
      }
    },
    getDs(num){
      if(parseInt(num)%2==0){
          return '双'
      }else{
          return '单'
      }
    },
    getkjdata(){
      Axios.get(this.kjurl+"&ttt="+Date.now())
      .then((response) => {
        this.kjdata = JSON.parse(response.data)
        this.oldissue =parseInt(this.kjdata[0].issue)
        this.dtime =parseInt(this.kjdata.countDown)
        this.daycount = parseInt(this.kjdata.daycount)
        if(this.newissue!=this.oldissue){
          // 判断是开奖了
          if(this.newissue == 0 ){
            this.newissue = this.oldissue
          }else{
            this.newissue = this.oldissue
              this.sendM()
              this.sendzdyc()
          }
        }
        localStorage.setItem('userdata', JSON.stringify(this.goup))
        localStorage.setItem('useradmin', this.superAdmin)
      })
      .catch((error) => {
        console.error(`problem with request: ${error}`);
      });
    },
    ycdata(index){
    // 处理算法
      let fgalg
      if(index == 1){
         fgalg = this.alg.split('+')
      }
      if(index == 2){
         fgalg = this.alg2.split('+')
      }
      if(index == 3){
         fgalg = this.alg3.split('+')
      }
      for (var i = 0; i < 415; i++) {
          let num = 0
          for(var j = 0;j<fgalg.length;j++){
              if(parseInt(fgalg[j]%4==0)){
                  num = num +this.getWs(parseInt(this.kjdata[i+parseInt(parseInt(fgalg[j])/4)]['c4']))
              }else{
                  num = num +parseInt(this.kjdata[i+parseInt(parseInt(fgalg[j])/4)]['c'+parseInt(fgalg[j])%4])
              }
          }
          if(num<10){
              num = '0'+num
          }else{
              num = String(num)
          }
          this.algdata[i] = num
      }
    },
    ycmsg(index){
      let sendMsg ="加拿大28-组合算法"+index+"预测"+'\n'
      let zql = 0
        for (let index = 0; index < this.daycount; index++) {
            let Dx = this.getDx(this.kjdata[index].c4)
            let Ds = this.getDs(this.kjdata[index].c4)
            let ycDx = this.getDx(this.algdata[index+1])
            let ycDs = this.getDs(this.algdata[index+1])
            if(ycDx == Dx || ycDs == Ds){
              zql++
            }
        }
        sendMsg +='今日预测正确率:'+parseInt(zql/this.daycount*100)+'%\n'+'===================='+'\n'
        for (var i = 0; i < 10; i++) {
            let isss = this.kjdata[9-i].issue
            let Dx = this.getDx(this.kjdata[9-i].c4)
            let Ds = this.getDs(this.kjdata[9-i].c4)
            let c4 = this.kjdata[9-i].c4
            let yc = this.algdata[10-i]
            let ycDx = this.getDx(this.algdata[10-i])
            let ycDs = this.getDs(this.algdata[10-i])
            var dc = ''
            if(ycDx == Dx || ycDs == Ds){
                dc = '正确'
            }else{
                dc = '错误'
            }
            sendMsg = sendMsg+isss.slice(4,isss.length)+'期:'+ycDx+' + '+ycDs+yc+'---'+c4+dc+'\n'
        }
        let newiss =String(parseInt(this.kjdata[0].issue)+1)
        sendMsg=sendMsg + newiss.slice(4,newiss.length)+'期:'+this.getDx(this.algdata[0])+' + '+this.getDs(this.algdata[0])+this.algdata[0]+'---'+'开奖中'
        return sendMsg
    },
    szycmsg(index){
          let sendMsg ="加拿大28-杀组算法"+index+"预测"+'\n'
          let dd = this.getDs(this.algdata[0])==='单'?'双':'单'
          let zql = 0
          for (let index = 0; index < this.daycount; index++) {
            let Dx = this.getDx(this.kjdata[index].c4)+this.getDs(this.kjdata[index].c4)
            let Ds = this.getDs(this.kjdata[index].c4)
            let ycDx = this.getDx(this.algdata[index+1])+this.getDs(this.algdata[index+1])
            let ycDs = this.getDs(this.algdata[index+1])==='单'?'双':'单'
            if(ycDx == Dx || ycDs == Ds){
              zql++
            }
          }
          sendMsg +='今日预测正确率:'+parseInt(zql/this.daycount*100)+'%\n'+'===================='+'\n'

          for (var i = 0; i < 10; i++) {
            let isss = this.kjdata[9-i].issue
            let Dx = this.getDx(this.kjdata[9-i].c4)+this.getDs(this.kjdata[9-i].c4)
            let Ds = this.getDs(this.kjdata[9-i].c4)
            let c4 = this.kjdata[9-i].c4
            let yc = this.algdata[10-i]
            let ycDx = this.getDx(this.algdata[10-i])+this.getDs(this.algdata[10-i])
            let ycDs = this.getDs(this.algdata[10-i])==='单'?'双':'单'
            var dc = ''
            if(ycDx == Dx || ycDs == Ds){
                dc = '正确'
            }else{
                dc = '错误'
            }
            sendMsg = sendMsg+isss.slice(4,isss.length)+'期:'+ycDx+' + '+ycDs+yc+'---'+c4+dc+'\n'
          }
        let newiss =(parseInt(this.kjdata[0].issue)+1).toString()
        sendMsg=sendMsg + newiss.slice(4,newiss.length)+'期:'+this.getDx(this.algdata[0])+this.getDs(this.algdata[0])+' + '+dd+this.algdata[0]+'---'+'开奖中'

        return sendMsg
    },
    // 开奖信息
    kjmsg(){
        let Dx = ' '+this.getDx(this.kjdata[0].c4)+' '
        let Ds = this.getDs(this.kjdata[0].c4)
        let c1 = this.kjdata[0].c1
        let c2 = this.kjdata[0].c2
        let c3 = this.kjdata[0].c3
        let c4 = this.kjdata[0].c4
        let sendMsg =this.newissue+"期开奖结果："+'==============='+'\n' + c1+' + ' +c2+' + ' +c3+' = '+ c4+" "+Dx+Ds+"\n"+'==============='+"\n"+this.lsmsg()
        return sendMsg
    },
    lsmsg(){
      let sendMsg = '历史:'
      for (var i = 0; i < 30; i++) {
          sendMsg = sendMsg + this.kjdata[29-i].c4+' '
      }
      return sendMsg
    },
    // 发送开奖信息
    sendM(){
      if(this.goup.length>0 && this.chat){
        let demogrou = [...this.goup]
        for(let i = 0; i < demogrou.length; i++){
          let sendmsg = ''
          if(demogrou[i].isvip){
            // 处理vip是否过期
            let vipdq = new Date(demogrou[i].vipdate)
            let thirtyDaysLater = new Date();
            thirtyDaysLater.setDate(thirtyDaysLater.getDate())
            let dq = new Date(thirtyDaysLater.toISOString().split('T')[0])
            let day = this.getDaysBetweenDates(dq,vipdq)
            if(day<=0){
              // 取消vip，重置
              demogrou[i].isvip=false
              demogrou[i].vipdate=""
            }
            // 是vip，使用群内设置发送
            if(!demogrou[i].iskj){
              continue;
            }else{
              if(day==1){
                sendmsg = this.kjmsg()+'\n'+'==============='+'\n'+demogrou[i].vipadv1+"\n"+"本群vip功能剩余1天，请及时续费"
              }else{
                sendmsg = this.kjmsg()+'\n'+'==============='+'\n'+demogrou[i].vipadv1
              }
            }
          }else{
            // 不是vip，使用免费群发送
            if(!this.iskj){
              continue;
            }else{
              sendmsg = this.kjmsg()+'\n'+'==============='+'\n'+this.adv1
            }
          }
          let message =  this.chat.createTextMessage({
            to:demogrou[i].id,
            conversationType:TencentCloudChat.TYPES.CONV_GROUP,
            payload: {
            text: sendmsg
            },
          })
          let promisesend = this.chat.sendMessage(message);
          promisesend.then(() =>{
            // 发送成功
            console.log("发送成功");
          }).catch(() =>{
            // 发送失败
            this.goup = this.goup.filter(item=>item.id!=demogrou[i].id)
          });
        }
      }
    },
    // 发送预测信息
    sendzdyc(){
      if(this.goup.length>0 && this.chat!= {}){
        for(let i = 0; i < this.goup.length; i++){
          let sendmsg=""
          if(this.goup[i].isvip){
            // 是vip，使用群内设置发送
            if(!this.goup[i].isszyc){
              continue;
            }else{
              this.ycdata(this.goup[i].algindex2)
              sendmsg = this.szycmsg(this.goup[i].algindex2)+'\n'+'===================='+'\n'+this.goup[i].vipadv2
            }
          }else{
            // 不是vip，使用免费群发送
            if(!this.isszyc){
              continue;
            }else{
              this.ycdata(this.algindex2)
              sendmsg = this.szycmsg(this.algindex2)+'\n'+'===================='+'\n'+this.adv2
            }
          }
          let message =  this.chat.createTextMessage({
            to:this.goup[i].id,
            conversationType:TencentCloudChat.TYPES.CONV_GROUP,
            payload: {
            text: sendmsg
            },
          })
          let promisesend = this.chat.sendMessage(message);
          promisesend.then(function(imResponse) {
            // 发送成功
            console.log("发送成功");
            console.log(imResponse);
          }).catch(function(imError) {
            // 发送失败
            console.log('sendMessage error:', imError);
          });
        }
      }
    },
    sendMsgG(msg,to){
      let message =  this.chat.createTextMessage({
            to:to,
            conversationType:TencentCloudChat.TYPES.CONV_GROUP,
            payload: {
            text: msg
            },
      })
      let promisesend = this.chat.sendMessage(message);
      promisesend.then(function(imResponse) {
        // 发送成功
        console.log("发送成功");
        console.log(imResponse);
      }).catch(function(imError) {
        // 发送失败
        console.log('sendMessage error:', imError);
      });
    },
    sendMsgC(msg,to){
      let message =  this.chat.createTextMessage({
            to:to,
            conversationType:TencentCloudChat.TYPES.CONV_C2C,
            payload: {
            text: msg
            },
      })
      let promisesend = this.chat.sendMessage(message);
      promisesend.then(function(imResponse) {
        // 发送成功
        console.log("发送成功");
        console.log(imResponse);
      }).catch(function(imError) {
        // 发送失败
        console.log('sendMessage error:', imError);
      });
    },
    sendMsgAt(msg,to,atUserList){
      let message =  this.chat.createTextAtMessage({
            to:to,
            conversationType:TencentCloudChat.TYPES.CONV_GROUP,
            payload: {
            text: msg,
            atUserList: atUserList
            },
      })
      let promisesend = this.chat.sendMessage(message);
      promisesend.then(function(imResponse) {
        // 发送成功
        console.log("发送成功");
        console.log(imResponse);
      }).catch(function(imError) {
        // 发送失败
        console.log('sendMessage error:', imError);
      });
    },
    tongji(){
        let tongjistr ="加拿大28-统计"+"\n"+'===================='+"\n"
        for (var i = 0; i < 14; i++) {
            let nnnum = i*2
            if(i<5){
                tongjistr+="0"+nnnum+"未开:"+this.kjdata['math'][nnnum]+"期   "+"0"+(nnnum+1)+"未开:"+this.kjdata['math'][nnnum+1]+"期"+"\n"
            }else{
                tongjistr+=nnnum+"未开:"+this.kjdata['math'][nnnum]+"期   "+(nnnum+1)+"未开:"+this.kjdata['math'][nnnum+1]+"期"+"\n"
            }
        }
        tongjistr +="大未开:"+this.kjdata['math']['da']+"期   " +"小未开:"+this.kjdata['math']['xiao']+"期" +"\n"
        
        tongjistr +="单未开:"+this.kjdata['math']['dan']+"期   " +"双未开:"+this.kjdata['math']['shuang']+"期" +"\n"
        
        tongjistr +="大双未开:"+this.kjdata['math']['ds']+"期   " +"大单未开:"+this.kjdata['math']['dd']+"期" +"\n"
        
        tongjistr +="小双未开:"+this.kjdata['math']['xs']+"期   " +"小单未开:"+this.kjdata['math']['xd']+"期" +"\n"
        
        tongjistr +="极小未开:"+this.kjdata['math']['jx']+"期   " +"极大未开:"+this.kjdata['math']['jd']+"期" +"\n"
         
        tongjistr +="对子未开:"+this.kjdata['math']['dz']+"期   " +"豹子未开:"+this.kjdata['math']['bao']+"期" 
        return tongjistr
      },
      // 获取当前时间
      getYYYYMMDD(ddd) {
          const now = ddd;
          const year = now.getFullYear();
          const month = (now.getMonth() + 1).toString().padStart(2, '0');
          const day = now.getDate().toString().padStart(2, '0');
          return year.toString() + month + day;
      },
      // 按数字分割
      splitByNumbers(str) {
        return str.split(/(\d+)/);
      },
      // 开头存在某个字符
      startsWithChar(str, char) {
        return str.charCodeAt(0) === char.charCodeAt(0);
      },
      // 去重数组
      mergeAndDedupeArrays(arr1, arr2) {
        const mergedArray = [...arr1, ...arr2];
        const mergedSet = new Set(mergedArray);
        return Array.from(mergedSet);
      },
      // 日期间隔
      getDaysBetweenDates(date1, date2) {
        const oneDay = 24 * 60 * 60 * 1000; // 一天的毫秒数
        const date1Ms = date1.getTime(); // 将日期转换为毫秒数
        const date2Ms = date2.getTime(); // 将日期转换为毫秒数
        const days = (date2Ms - date1Ms) / oneDay; // 计算天数
        return Math.floor(days); // 返回天数，可以选择是否四舍五入
      },
      helpMsg(isvip){
        let srt = "机器人指令："+"\n"+"①：[1][杀组]--查看杀组预测"+"\n"+"②：[2][预测]--查看组合预测"+"\n"+"③：[3][走势][历史]--查看历史"+"\n"+"④：[4][数据][统计]--查看往期开奖数据统计"

        if(isvip === true){
          srt += "\n"+"⑤：[预测1|2|3]--查看组合算法1|2|3预测"+"\n"+"⑥：[杀组1|2|3]--查看杀组算法1|2|3预测"+"\n"+"⑦：[签到]--每日群内签到"
        }
        return srt;
      },
      async getqiandao(qunid,index,day){
        return new Promise((resolve)=>{
          let menumlist = []
          for(let i = 0;i<6000/30;i++){
            let promise =  this.chat.getGroupMemberList({
            groupID: qunid,
            count: 1,
            offset:i
            }); // 从0开始拉取30个群成员
            promise.then((imResponse)=> {
              if(imResponse.data.memberList.length !=0){
                // 判断是否签到
                let user = imResponse.data.memberList[0].userID
                let isqiandao = false
                for(let y =0;y<day;y++){
                  let currentDate = new Date();
                  currentDate.setDate(currentDate.getDate() - y);  // 获取前一天的日期
                  let curr  = this.getYYYYMMDD(currentDate)
                  let qiandaoU = this.goup[index].qiandao.filter(item=>item.userId===user && item.date === curr)
                  if(qiandaoU.length > 0){
                    // 存在*天签到，返回true
                    isqiandao = true
                    break;
                  }else{
                    // 不存在*天签到，返回false
                    isqiandao = false
                  }
                }
                if(!isqiandao){
                  // 不存在*天签到
                  if(user != this.userid){
                    menumlist.push(user)
                  }
                }
              }
              
            }).catch((imError)=> {
              console.warn('getGroupMemberList error:', imError);
            });
          }
          resolve(menumlist)
        })
      },
      async getnum(qunid,index,day){
        try{
          const data = await this.getqiandao(qunid,index,day)
          return data;
        } catch(err){
          console.log(err)
        }
      },
  },
  // 实例挂载完成
  mounted() {
    // 读取配置
    // 初始化
    // 先登录 后初始化
    this.Worker2 = new myWorker()
    this.Worker2.postMessage({
        type: "start",
        interval: 1000
    })
    // 获取开奖
    this.Worker2.onmessage = (event)=> {
     // 收息消息
      if (event.data.type == 'message') {
          // TODO: 收到消息后
          // 定时任务//遍历
          let dqdate = moment().format('YYYY-MM-DD HH:mm:ss')
          this.dtime = parseInt(this.dtime)-1
          if(this.dtime <= 0 && this.dtime%5 == 0){
             this.getkjdata()
          }
          this.goup.forEach((item,indexy)=>{
            if(item.isvip){
              // 是vip
              // 遍历定时任务
              item.dingshi.forEach((list,indexx)=>{
                let rwdate = moment(list.date).add(parseInt(list.ss),'seconds').format('YYYY-MM-DD HH:mm:ss')
                if(rwdate == dqdate){
                  // 到了定时时间
                  // 发送
                  this.sendMsgG(list.text,item.id)
                   // 修改任务时间
                   this.goup[indexy].dingshi[indexx].date = dqdate
                }
              })
            }
          })
      }
    }
    // 获取群组 
  },
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
